package com.lcc.lynx.base.system.controller;

import com.lcc.lynx.base.system.service.SysConfigService;
import com.lcc.lynx.common.ResultVO;
import com.lcc.lynx.common.cache.GroupCache;
import com.lcc.lynx.common.cache.enums.CacheEnum;
import com.lcc.lynx.jdbc.IByIdController;
import com.lcc.lynx.security.annotation.Permission;
import com.lcc.lynx.base.system.pojo.dv.SysConfigDV;
import com.lcc.lynx.common.utils.StrUtil;
import lombok.AllArgsConstructor;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import java.util.Arrays;
import java.util.List;

/**
 * 系统配置控制实现
 * @auth lcc
 * @date 2025-01-01
 */
@RestController
@AllArgsConstructor
@RequestMapping("/sys/config")
public class SysConfigController implements IByIdController<SysConfigDV> {

    private SysConfigService service;
    

    @Override
    public SysConfigService getService() {
       return service;
    }

    @Override
    @Permission(Permission.upd)
    public ResultVO<?> update(SysConfigDV dto) {
        ResultVO<?> update = IByIdController.super.update(dto);
        GroupCache.delVal(CacheEnum.config + dto.getCfgKey());
        return update;
    }

    @Override
    @ResponseBody
    @Permission(Permission.del)
    public ResultVO<?> delete(String idList) {
        if(StrUtil.isEmpty(idList)){
            return ResultVO.fail("idList不能为空");
        }
        List<String> list = Arrays.asList(idList.split(","));
        SysConfigDV sysConfigDV = service.selectById(list.get(0));
        if(sysConfigDV != null){
            GroupCache.delVal(CacheEnum.config + sysConfigDV.getCfgKey());
        }
        return getService().deleteByIdList(list) == 0 ? ResultVO.fail("删除失败") : ResultVO.success("成功");
    }
}